package com.exp2.caloriedaily

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.DatePicker
import com.exp2.caloriedaily.adapter.CalorieListAdapter
import com.exp2.caloriedaily.domain.CalorieData
import com.exp2.caloriedaily.service.CalorieService
import kotlinx.android.synthetic.main.activity_manage_calorie.*
import kotlinx.android.synthetic.main.activity_update__calorie_.*
import org.jetbrains.anko.alert
import org.jetbrains.anko.noButton
import org.jetbrains.anko.yesButton

class UpdateCalorieActivity : AppCompatActivity() {
    var date=""


    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_update__calorie_)

        addCalorie.setOnClickListener {
            updateCalorie()
        }
    }

    fun setDate() {
        val year=datePicker.year
        val dayOfMonth=datePicker.dayOfMonth
        val monthOfYear=datePicker.month
        if (monthOfYear+1<10&&dayOfMonth<10){
            date = "$year-0${monthOfYear + 1}-0$dayOfMonth"
        }else if (monthOfYear+1>=10&&dayOfMonth<10){
            date = "$year-${monthOfYear + 1}-0$dayOfMonth"
        }else if (monthOfYear+1<10&&dayOfMonth>=10){
            date = "$year-0${monthOfYear + 1}-$dayOfMonth"
        }else{
            date = "$year-${monthOfYear + 1}-$dayOfMonth"
        }
    }

    fun updateCalorie() {
        setDate()
        var calorie=CalorieData()
        var data:String=editText.text.toString()
        if (data.isEmpty()||data[0]=='0'){
            alert("数据格式错误！") {
                yesButton {
                } }.show()
            return
        }

        calorie.date=date
        calorie.calorie=data.toInt()

        val calorieService:CalorieService= CalorieService.getInstance(this)
        calorieService.insert(calorie)

        this.finish()
    }


}